package asserts;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.google.common.base.Objects;
import common.TestStep;
import exceptions.ErrorException;
import io.qameta.allure.Allure;
import io.restassured.RestAssured;
import org.apache.log4j.Logger;
import org.testng.*;

import java.io.IOException;
import java.math.BigDecimal;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static common.TestStep.getassertInfolList;
import static common.TestStep.requestAndRespondBody;

/*
 *数据库断言及响应报文断言
 */
public class Asserts {
    //收集断言异常用于报表日志展示
    public static List<Error> errors = new ArrayList<Error>();
    //收集断言信息文本，用于报表展示
    public static List<String> assertInfolList = new ArrayList<String>();
    //收集信息文本用于报表展示
    public static List<String> messageList = new ArrayList<String>();
    //记录错误数量
    public static Integer errorIndex = 0;
    static Logger log = Logger.getLogger(Asserts.class);

    public static void asserts(JSONObject Body, String response, Map<String, String> map) throws IOException, ClassNotFoundException {

        //测试报告展现请求报文 响应报文
        String url = RestAssured.baseURI + ":" + RestAssured.port + RestAssured.basePath;

        requestAndRespondBody(url, Body, response);
        String preResult = "";
        //格式化json串
        boolean prettyFormat = true; //格式化输出
        JSONObject jsonObject = JSONObject.parseObject(response);

        response = JSONObject.toJSONString(jsonObject, prettyFormat);

        log.info("响应报文：" + response);
    }



    /*
     * 验证实际值与预期值是否相等
     *
     *  @author Administrator gujinle
     */
    public static boolean VerityString(String actual, String exceptStr) {
        String verityStr = "Assert验证：{" + "实际值：" + actual + "," + "预期值" + exceptStr + "} 实际值与预期值是否一致";
        log.info(verityStr);
        boolean flag = true;
        try {
            Assert.assertEquals(actual, exceptStr);
            AssertPassLog();
            flag = true;
        } catch (Error e) {
            flag = false;
            AssertFailedLog();
            errorIndex++;
            errors.add(e);
            assertInfolList.add(verityStr + ":failed");
            getassertInfolList();
        }
        return flag;
    }

    /*
     * 验证content數組与预期值是否相等
     *  @author Administrator gujinle

     */

    //   json如下 {
//        "code": 1,
//            "data": {
//        "content": [
//        {
//            "enabledAe": 0,
//                "finished": 0,
//                "followupPlan": "2",
//                "isAudit": "1",
//                "isRandom": 2,
//                "isStartUpPeriod": "1",
//                "orgId": "75635",
//                "orgName": "广州医科大学附属第一医院",
//                "processing": 0,
//                "projectCenter": "1",
//                "projectCreator": "4467143",
//                "projectCreatorName": "罗群",
//                "projectId": "1374902221551775746",
//                "projectLock": 0,
//                "projectName": "系统随访项目",
//                "projectTypeName": "队列性研究",
//                "randomGroupType": "0",
//                "removed": 0,
//                "resourceId": "1374902223552458754",
//                "status": "10",
//                "terminated": 0,
//                "userDataRId": "1374902223380492290",
//                "userStatus": "1"
//        }
//        ],
//        "empty": false,
//                "first": true,
//                "last": false,
//                "number": 0,
//                "numberOfElements": 2,
//                "pageable": {
//            "offset": "0",
//                    "pageNumber": 0,
//                    "pageSize": 2,
//                    "paged": true,
//                    "sort": {
//                "empty": true,
//                        "sorted": false,
//                        "unsorted": true
//            },
//            "unpaged": false
//        },
//        "size": 2,
//                "sort": {
//            "empty": true,
//                    "sorted": false,
//                    "unsorted": true
//        },
//        "totalElements": "500",
//                "totalPages": 250
//    },
//        "msg": "成功"
//    }
    public static boolean VerityArray(JSONArray contentArr, List<String> arr1, String projectName) {
        List Values = new ArrayList();
        List comparingList = new ArrayList();
        boolean flag = true;
        for (int index = 0; index < contentArr.size(); index++) {
            JSONObject content = contentArr.getJSONObject(index);
            String Value = content.getString(projectName);
//            System.out.println(projectName);
//           if(projectName.equals("支气管扩张患者用药随访")){
//               System.out.println(projectName);
//               break;
//           }
//            ArrayList<String> projectNames=new ArrayList<String>();
            Values.add(Value);
        }
        String verityStr = "Assert验证：{字段" + projectName + "实际值：" + Values + "\n" + "预期值" + arr1 + "} 实际值与预期值是否一致";
        for (String p : arr1) {
            int mark = 0;
            for (Object q : Values)
                if (q.toString().equals(p)) {
                    mark++;
                }
            if (mark == 0) {
                flag = false;
                AssertFailedLog();
                errorIndex++;
                assertInfolList.add(verityStr + ":failed" + "\n");
                comparingList.add(p);
                String errorstr = ("缺少的" + projectName + "是" + comparingList);
                log.error(errorstr);
                assertInfolList.add(errorstr);
            } else {
                AssertPassLog();
                flag = true;
            }

        }
        getassertInfolList();
        return flag;
    }


    /**
     *比较2个list的不同，并把不同的数据输出来
     * @param menulist         获取的菜单列
     * @param resourcelist     指标参考列--读取excel提供的数据
     */
    public static boolean VerityListDiff(List<String> menulist, List<String> resourcelist){
        boolean flag ;
        List lacklist=new ArrayList();     //缺少的指标列
        List list=new ArrayList();         //增加的指标列
        int mark = 0;

        /*先找出增加的指标
        * */
        for (String str : menulist) {
            if (!resourcelist.contains(str)) {
                list.add(str);
                mark++;
            }
        }

        /*再找出缺少的指标
        * */
        for (String str : resourcelist) {
            if (!menulist.contains(str)) {
                lacklist.add(str);
                mark++;
            }

        }
//        String lackstr=("缺少的指标："+lacklist);
//        String increasestr=("增加的指标："+list);

        /*后续对flag和数据的处理
        * */
        if (mark != 0) {
            flag = false;
            AssertFailedLog();
            errorIndex++;
            if (lacklist.size() > 0 && list.size() > 0) {
                assertInfolList.add("缺少的指标：");
                assertInfolList.addAll(lacklist);
                assertInfolList.add("增加的指标：");
                assertInfolList.addAll(list);
            } else if (list.size() > 0) {
                assertInfolList.add("增加的指标：");
                assertInfolList.addAll(list);
            } else if (lacklist.size() > 0) {
                assertInfolList.add("缺少的指标：");
                assertInfolList.addAll(lacklist);
            }
        } else {
            AssertPassLog();
            flag = true;
        }
        getassertInfolList();
        return flag;
    }



    /*单病种质控系统
     * 判断json数组数目不为空
     * */
    public static void VerityLength(JSONArray contentArr) {
      Assert.assertNotEquals(contentArr.size(), 0);

    }

    /*单病种质控系统
     *终末质控-按年查询
     * 比对rowsjson数组里第一组key:group,value的值
     * */
    public static boolean VerityValue(JSONArray contentArr) {
        boolean flag = true;
        int mark = 0;
        List<String> chartType = new ArrayList<>();
        for (int index = 0; index < contentArr.size(); index++) {
            JSONObject content = contentArr.getJSONObject(index);
//            JSONObject dataobj = content.getJSONObject("data");
//            JSONArray rows = dataobj.getJSONArray("rows");
//            JSONObject object = rows.getJSONObject(0);
//            String value1 = object.getString(value);

            chartType.add(content.getString("chartType"));

            switch (content.getString("chartType")) {
                case "line":  //线
//                    Allure.addAttachment("chartType:line判断依据","判断rows数据集不为空和键名为value的值不全部为0");
                    JSONObject dataobj1 = content.getJSONObject("data");
                    JSONArray rows1 = dataobj1.getJSONArray("rows");
                    if(rows1 != null && rows1.size() > 0){
                        for (Object ob:rows1){
                            JSONObject jb=(JSONObject)ob;
                            if(jb.containsKey("value")){
                                String value1=jb.getString("value");
                                log.info("获取到value的值是：" + value1);
                                if (value1 != null && !Objects.equal(value1,0)){
                                    mark=0;
                                    break;
                                }else{
                                    mark++;
                                }

                            }else {
                                mark++;
                            }
                        }
                    }else{
                        mark++;
                        log.info("rows数据集为空");
                    }
                    break;
                case "table":  //表格
//                    Allure.addAttachment("chartType:table判断依据","判断rows数据集不为空");
                    JSONObject dataobj2 = content.getJSONObject("data");
                    JSONArray rows2 = dataobj2.getJSONArray("rows");
//                    JSONObject object2 = rows2.getJSONObject(0);
//                    String PER = object2.getString("PER");
//                    log.info("获取到PER的值是：" + PER);
//                    if (PER == "") {
//                        mark++;
//                    }
                    if(rows2 != null && rows2.size() > 0){
                        mark=0;
                    }else {
                        mark++;
                        log.info("rows数据集为空");
                    }
                    break;
                case "gauge": //仪表
//                    Allure.addAttachment("chartType:gauge判断依据","判断rows数据集不为空和键名为value的值不全部为0");
                    JSONObject dataobj3 = content.getJSONObject("data");
                    JSONArray rows3 = dataobj3.getJSONArray("rows");
                    if(rows3 != null && rows3.size() > 0){
                        for (Object ob:rows3){
                            JSONObject jb=(JSONObject)ob;
                            if(jb.containsKey("value")){
                                String value3=jb.getString("value");
                                log.info("获取到value的值是：" + value3);
                                if (value3 != null && !Objects.equal(value3,0)){
                                    mark=0;
                                    break;
                                }else{
                                    mark++;
                                }

                            }else {
                                mark++;
                            }
                        }
                    }else{
                        mark++;
                        log.info("rows数据集为空");
                    }



                    break;
                case "tiled-bar":
//                    Allure.addAttachment("chartType:tiled-bar判断依据","判断rows数据集不为空和键名为count的值大于0");
                    JSONObject dataobj4 = content.getJSONObject("data");
                    JSONArray rows4 = dataobj4.getJSONArray("rows");
                    if(rows4 != null && rows4.size() > 0){
                        for(Object ob:rows4) {
                            JSONObject jb = (JSONObject) ob;
                            if(jb.containsKey("count")){
                                int  count = jb.getIntValue("count");
                                log.info("获取到count的值是：" + count);
                                if(count >0){
                                    mark=0;
                                    break;
                                }else {
                                    mark++;
                                }
                            }
                        }

                    }else{
                        mark++;
                        log.info("rows数据集为空");
                    }

                    break;
                case "text":     //文本格式
                    JSONObject dataobj5 = content.getJSONObject("data");
                    JSONArray rows5 = dataobj5.getJSONArray("rows");
                    JSONObject object5 = rows5.getJSONObject(0);
                    String count2 = object5.getString("count");
                    log.info("获取到count的值是：" + count2);
                    if (count2 == "") {
                        mark++;
                    }
                    break;

                case "horizon-bar":
                    JSONObject dataobj6 = content.getJSONObject("data");
                    JSONArray rows6 = dataobj6.getJSONArray("rows");
                    JSONObject object6 = rows6.getJSONObject(0);
                    String count3 = object6.getString("count");
                    log.info("获取到count的值是：" + count3);
                    if (count3 == "") {
                        mark++;
                    }
                    break;
                case "pie":  //饼图
                    JSONObject dataobj7 = content.getJSONObject("data");
                    JSONArray rows7 = dataobj7.getJSONArray("rows");

                    if(rows7 != null && rows7.size() > 0){
                        for(Object ob:rows7) {
                            JSONObject jb = (JSONObject) ob;
                            if(jb.containsKey("count")){
                                int  count = jb.getIntValue("count");
                                log.info("获取到count的值是：" + count);
                                if(count >0){
                                    mark=0;
                                    break;

                                }else {
                                    mark++;
                                }
                            }
                        }

                    }else{
                        mark++;
                        log.info("rows数据集为空");
                    }

                    break;


//                    JSONObject object7 = rows7.getJSONObject(0);
//                    String count4 = object7.getString("count");
//                    log.info("获取到count的值是：" + count4);
//                    if (count4 == "") {
//                        mark++;
//                    }
//                    break;

                case "bar":
//                    Allure.addAttachment("chartType:bar判断依据","判断rows数据集不为空和键名为count的值大于0");
                    JSONObject dataobj8 = content.getJSONObject("data");
                    JSONArray rows8 = dataobj8.getJSONArray("rows");
                    if (rows8 != null && rows8.size() > 0) {
                        for (Object ob : rows8) {
                            JSONObject jb = (JSONObject) ob;
                            JSONArray rows9 = jb.getJSONArray("rows");
                            if(rows9 != null && rows9.size()>0){
                                for (Object object:rows9) {
                                    JSONObject jb1 = (JSONObject) object;
                                    if (jb1.containsKey("count")) {
                                        int count5 = jb1.getIntValue("count");
                                        log.info("获取到count的值是：" + count5);
                                        if (count5 > 0) {
                                            mark = 0;
                                            break;
                                        } else {
                                            mark++;
                                        }
                                    } else {
                                        mark++;
                                    }
                                }
                            }


                        }


                    } else {
                        mark++;
                        log.info("rows数据集为空");
                    }
                    break;
                case "multi-line":   //趋势图
                    JSONObject dataobj9 = content.getJSONObject("data");
                    JSONArray rows9 = dataobj9.getJSONArray("rows");
                    JSONObject object9 = rows9.getJSONObject(0);
                    JSONArray rows10 = object9.getJSONArray("rows");
                    JSONObject object10 = rows10.getJSONObject(0);
                    String value = object10.getString("value");
                    log.info("获取到value的值是：" + value);
                    if (value == "") {
                        mark++;
                    }
                    break;

                case "stack-bar":   //堆条
                    JSONObject dataobj10 = content.getJSONObject("data");
                    JSONArray rows11 = dataobj10.getJSONArray("rows");
                    JSONObject object11 = rows11.getJSONObject(0);
                    String value2 = object11.getString("value");
                    log.info("获取到value的值是：" + value2);
                    if (value2 == "") {
                        mark++;
                    }
                    break;

                case "liquidfill":   //充夜
                    JSONObject dataobj11 = content.getJSONObject("data");
                    JSONArray rows12 = dataobj11.getJSONArray("rows");
                    JSONObject object12 = rows12.getJSONObject(0);
                    String value4 = object12.getString("value");
                    log.info("获取到value的值是：" + value4);
                    if (value4 == "") {
                        mark++;
                    }
                    break;

                case "barchart-h":   //条形图
                    JSONObject dataobj12 = content.getJSONObject("data");
                    JSONArray rows13 = dataobj12.getJSONArray("rows");
                    JSONObject object13 = rows13.getJSONObject(0);
                    String value5 = object13.getString("value");
                    log.info("获取到value的值是：" + value5);
                    if (value5 == "") {
                        mark++;
                    }
                    break;

                case "boxplot":   //图
                    JSONObject dataobj13 = content.getJSONObject("data");
                    JSONArray rows14 = dataobj13.getJSONArray("rows");
                    log.info("rows的数组是"+rows14.toString());
                    if (rows14.size() == 0) {
                        mark++;
                    }
                    break;
                default:
                    System.out.println("匹配不到chartType类型");

            }

        }
        chartType = chartType.stream().distinct().collect(Collectors.toList());
        for (String str : chartType) {
            if (Objects.equal("line", str)) {
                Allure.addAttachment("chartType:line判断依据","判断rows数据集不为空和键名为value的值不全部为0");
            } else if (Objects.equal("table", str)) {
                Allure.addAttachment("chartType:table判断依据","判断rows数据集不为空");
            } else if (Objects.equal("gauge", str)) {
                Allure.addAttachment("chartType:gauge判断依据","判断rows数据集不为空和键名为value的值不全部为0");
            } else if (Objects.equal("tiled-bar", str)) {
                Allure.addAttachment("chartType:tiled-bar判断依据","判断rows数据集不为空和键名为count的值大于0");
            } else if (Objects.equal("bar", str)) {
                Allure.addAttachment("chartType:bar判断依据","判断rows数据集不为空和键名为count的值大于0");
            }
            else if (Objects.equal("pie", str)) {
                Allure.addAttachment("chartType:pie判断依据","判断rows数据集不为空和键名为count的值大于0");
            }
        }
        if (mark == 0) {
            flag = true;
            AssertPassLog();

        } else {
            AssertFailedLog();
            flag = false;
        }
        return flag;
    }

/*病人个人住院质控*/
public static boolean VerityPersonalReport(JSONArray contentArr) {
    boolean flag = true;
    int mark = 0;
    List<String> chartType = new ArrayList<>();
    for (int index = 0; index < contentArr.size(); index++) {
        JSONObject content = contentArr.getJSONObject(index);
        switch (content.getString("chartType")) {
            case "text":     //文本格式
                JSONObject dataobj5 = content.getJSONObject("data");
                JSONArray rows5 = dataobj5.getJSONArray("rows");
                JSONObject object5 = rows5.getJSONObject(0);
                String count2 = object5.getString("count");
                log.info("获取到count的值是："+count2);
                if (count2 == "") {
                    mark++;
                }
                break;
            case "table":  //表格
                JSONObject dataobj2 = content.getJSONObject("data");
                JSONArray rows2 = dataobj2.getJSONArray("rows");
                JSONObject object2 = rows2.getJSONObject(0);
                String s1 = object2.getString("急诊纪录");
                String s2 = object2.getString("出院前1~2周");
                String s3 = object2.getString("治疗72h之后");
                String s4 = object2.getString("入院72小时内");
                String s5 = object2.getString("入院24h内");
                String s6 = object2.getString("出院日");
                if (s1 == null&&s2 == null&&s3 == null&&s4 == null&&s5 == null&&s6 == null) {
                    mark++;
                    log.info("获取到的键值均为Null");
                }
                break;
            case "bar":     //条形格式
                JSONObject dataobj1 = content.getJSONObject("data");
                JSONArray rows1 = dataobj1.getJSONArray("rows");
                if(rows1 != null && rows1.size() > 0){
                    for(Object ob:rows1) {
                        JSONObject jb = (JSONObject) ob;
                        if(jb.containsKey("count")){
                            int  count = jb.getIntValue("count");
                            log.info("获取到count的值是：" + count);
                            if(count >0){
                                mark=0;
                                break;
                            }else {
                                mark++;
                            }
                        }
                    }
                }else{
                    mark++;
                    log.info("rows数据集为空");
                }
                break;
            case "multi-line":   //趋势图
                JSONObject dataobj9 = content.getJSONObject("data");
                JSONArray rows9 = dataobj9.getJSONArray("rows");
                JSONObject object9 = rows9.getJSONObject(0);
                JSONArray rows10 = object9.getJSONArray("rows");
                JSONObject object10 = rows10.getJSONObject(0);
                String value = object10.getString("value");
                log.info("获取到value的值是："+value);
                if (value == "") {
                    mark++;
                }
        }
    }
    chartType = chartType.stream().distinct().collect(Collectors.toList());
    for (String str : chartType) {
        if (Objects.equal("bar", str)) {
            Allure.addAttachment("chartType:bar判断依据","判断rows数据集不为空和键名为count的值不全部为0");
        }
    }
    if (mark == 0) {
        flag = true;
        AssertPassLog();

    } else {
        AssertFailedLog();
        flag = false;
    }
    return flag;
}





    //断言成功日志内容
    private static void AssertPassLog() {
        log.info("【Assert验证  pass!】");
    }

    //断言失败日志内容
    private static void AssertFailedLog() {
        log.error("【Assert验证  failed!】");
    }

    //断言日志内容
    private static void AssertLog(String str1, String str2) {
        log.info("【Assert验证】:" + "判断[比较]" + "{" + str1 + "," + str2 + "}" + "是否一致[相等]");
    }


}
